import O1HD, { managers } from "../../engines/gameEngine";
import { GAME_MODE } from "../../engines/managers/gameContent/gameMode_json";
import { PAGE } from "../Enum";
import { IPagePramsBase } from "../Interface";
import mainRoot from "../MainRoot";

export default class PageBase extends cc.Component {
    public pageName: PAGE;
    protected m_mode: GAME_MODE = GAME_MODE.NORMAL;
    protected registed: boolean = false;
    protected content: cc.Node = null;
    protected btnRoot: cc.Node = null;
    protected onClose: () => void = null;
    protected onReward: () => void = null;
    /** 防止连续点击 */
    public static isPlaySound: boolean = false;

    async OnPageOpen(prams: IPagePramsBase) {
        prams && (this.m_mode = prams.mode);
        prams && (this.onReward = prams.onReward);
        prams && (this.onClose = prams.onClose);
        this.node.opacity = 0;
        if (!this.registed) {
            this.AddEvent();
        }
        this.content = this.node.getChildByName("Content");
        this.btnRoot = this.node.getChildByName("ButtonRoot");
        this.btnRoot && this.btnRoot.children.forEach((node) => {
            node.opacity = 0;
        });
        if (this.content) {
            this.content.scale = 0;
        }
        if (prams && prams.index == 112 && prams.loseText != undefined) {
            let labelName: string = "Label" + prams.index;
            this.content.getChildByName(labelName).getComponentInChildren(cc.Label).string = prams.loseText;
        }
        if (prams && prams.index == 113 && prams.loseText != undefined) {
            let labelName: string = "Label" + prams.index;
            this.content.getChildByName(labelName).getComponentInChildren(cc.Label).string = prams.loseText;
        }
        if (prams && prams.index == 147 && prams.loseText != undefined) {
            let labelName: string = "Label" + prams.index;
            this.content.getChildByName(labelName).getComponentInChildren(cc.Label).string = prams.loseText;
        }
        cc.tween(this.node).to(0.25, { opacity: 255 }).start();
        // this.Log("打开");
        if (this.content) {
            await new Promise<void>((resolve) => {
                cc.tween(this.content).delay(0.2).call(() => {
                    // O1HD.getManagers().soundMgr.playSound("tanchuang");
                }).to(0.3, { scale: 1 }, { easing: 'backOut' }).call(resolve).start();
            });
        }
        if (this.btnRoot) {
            for (let index = 0; index < this.btnRoot.children.length; index++) {
                const element = this.btnRoot.children[index];
                await new Promise<void>((resolve, reject) => {
                    cc.tween(element).to(0.2, { opacity: 255 }).call(resolve).start();
                });
            }
        }
    }

    ShowPage() {
        this.node.active = true;
    }

    HidePage() {
        this.node.active = false;
        // this.OnPageClose();
        // mainRoot.UISystem.HidePage(this.pageName);
    }

    ClosePage() {
        mainRoot.UISystem.ClosePage(this.pageName);
    }

    OnPageClose() {
        this.RemoveEvent();
        // this.Log("关闭页面");
        this.onClose && this.onClose();
        this.onClose = null;
        this.onReward = null;
        if (this.node.isValid) {
            cc.tween(this.node).to(0.1, { opacity: 0 }).call(() => {
                this.node.destroy();
            }).start();
        }
    }

    async BackMain() {
        await O1HD.getManagers().gameMgr.returnMainScene();
        mainRoot.UISystem.CloseAllPage();
    }

    async OnMainBtn() {
        managers().soundMgr.playSound('mainSound/anNiu');
        await this.BackMain();
        // if(this.m_mode == GAME_MODE.BHMB||this.m_mode == GAME_MODE.CJTT){
        //     mainRoot.UISystem.OpenPage(PAGE.LEVELSKINMENU,{
        //         mode:this.m_mode
        //     });
        // }
        // else{
        mainRoot.UISystem.OpenPage(PAGE.LEVELMENU, {
            mode: this.m_mode
        });
        // }

    }

    OnCloseBtn() {
        managers().soundMgr.playSound('mainSound/anNiu');
        this.ClosePage();
    }

    AddEvent() {
        this.registed = true;
        // this.Log("注册事件");
    }
    RemoveEvent() {
        // this.Log("注销事件");
        this.registed = false;
    }

    Log(...prams) {
        console.log(`${this.pageName}:--->>[\n`, ...prams, "\n]");
    }
    Warn(...prams) {
        console.warn(`${this.pageName}:---[\n`, ...prams, "\n]");
    }

    onDestroy() {

    }
}